<?php
$playerId = $_GET['playerId'];
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">
  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./update.php?playerId=<?php echo $playerId; ?>" method="POST" lay-filter="update">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">球员名</label>
        <div class="layui-input-block">
          <input type="text" name="playerName" maxlength="10" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">身高</label>
        <div class="layui-input-block" style="width: 100px;">
          <!-- 身高在150-220之间 -->
          <input type="text" name="height" pattern="^(1[5-9][0-9]|1[0-9]{2}|200|20[1-9]|21[0-9]|220)$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">年龄</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input type="text" name="age" pattern="^(1[8-9]|[2-4][0-9]|50)$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">号码</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input type="text" name="num" pattern="^[0-9]{1,2}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">位置</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input name="pos" autocomplete="off" maxlength="8" class="layui-input" required>
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">年薪</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input name="salary" autocomplete="off" pattern="^\d{0,5}$" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">俱乐部</label>
        <div class="layui-input-inline">
          <input name="club" autocomplete="off" maxlength="10" class="layui-input" required>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">进球数</label>
        <div class="layui-input-inline" style="width: 150px;">
          <input type="text" name="score" pattern="^[0-9]{1,2}$" autocomplete="off" class="layui-input" required>
        </div>
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">球员描述</label>
      <div class="layui-input-block">
        <textarea name="desc" maxlength="300" class="layui-textarea"></textarea>
      </div>
    </div>

    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <?php
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  $playerSql = "select * from player where player_id = {$playerId};";
  $playerAaary = mysqli_query($dbc, $playerSql);
  $rows = mysqli_fetch_assoc($playerAaary);
  ?>
  <script>
    let res = <?php echo json_encode($rows); ?>;
    // console.log(res);
    layui.use(function() {
      var $ = layui.$;
      var form = layui.form;
      // 表单赋值
      form.val('update', {
        "playerName": res.player_name,
        "height": res.height,
        "age": res.player_age,
        "num": res.player_num,
        "pos": res.player_pos,
        "salary": res.player_salary,
        "club": res.player_club,
        "score": res.player_score,
        "desc": res.player_desc
      });
    })
  </script>

</body>

</html>